package com.vdong.common.oauth.common.function;

import com.github.pagehelper.PageInfo;
import com.vdong.common.oauth.common.enums.ResultCodeEnum;
import com.vdong.common.oauth.common.pojo.ResultCodeInter;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @program: kpay-basics-srv
 * @description:
 * @author: Mr.Zhang
 * @create: 2018-06-15 08:46
 **/
public class APIResult<T> extends LinkedHashMap<String, Object> implements Serializable {

    private static final String CODE = "code";
    private static final String SUCCESS = "success";
    private static final String MESSAGE = "message";
    private static final String DEFDATA = "data";
    private static final String EXT_PAGE = "page";
    private static final String EXT_SIZE = "size";
    private static final String EXT_ITEMS = "items";
    private static final String EXT_PAGES = "pages";
    private static final String EXT_TOTAL = "total";

    private Boolean success;

    private String message;

    private T data;

    private List<T> items;

    public APIResult() {
        super();
    }

    public APIResult(Boolean isOk) {
        this.success = isOk;
        put(SUCCESS, isOk);
    }

    public APIResult(ResultCodeInter rci) {
        this.success = ResultCodeEnum.SUCCESS.getCode().equals(rci.getCode());
        this.message = rci.getMessage();
        put(SUCCESS, this.success);
        put(CODE, rci.getCode());
        put(MESSAGE, rci.getMessage());
    }

    public APIResult(ResultCodeInter rci, String message) {
        this.success = ResultCodeEnum.SUCCESS.getCode().equals(rci.getCode());
        this.message = message;
        put(SUCCESS, this.success);
        put(CODE, rci.getCode());
        put(MESSAGE, message);
    }

    public APIResult<T> data(List<T> items) {
        put(EXT_ITEMS, items);
        this.items = items;
        return this;
    }

    public APIResult<T> data(T data) {
        put(DEFDATA, data);
        this.data = data;
        return this;
    }

    public APIResult<T> data(String key, Object data) {
        put(key, data);
        return this;
    }

    public APIResult data(PageInfo<T> pageInfo) {
        this.items = pageInfo.getList();
        put(EXT_TOTAL, pageInfo.getTotal());
        put(EXT_ITEMS, pageInfo.getList());
        put(EXT_PAGE, pageInfo.getPageNum());
        put(EXT_SIZE, pageInfo.getPageSize());
        put(EXT_PAGES, pageInfo.getPages());
        return this;
    }

    public Boolean isSuccess() {
        return this.success;
    }

    public T getData() {
        return this.data;
    }

    public List<T> getItems() {
        return this.items;
    }

}
